from rest_framework import serializers
from .models import *


class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"
        # fields = ['title']


class BannerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Banner
        fields = ['carousel_id', 'carousel_url', 'redirect_url', 'carousel_rank']


class MallUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = MallUser
        fields = "__all__"


class UserAddressSerializer(serializers.ModelSerializer):
    address_id = serializers.IntegerField(read_only=True)

    class Meta:
        model = UserAddress
        fields = "__all__"


class GoodsInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsInfo
        fields = "__all__"


class GoodsCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = "__all__"


class GridImgSerializer(serializers.ModelSerializer):
    class Meta:
        model = GridImg
        fields = "__all__"


class ShoppingCartSerializer(serializers.ModelSerializer):
    # cart_item_id = serializers.IntegerField(read_only=True)
    class Meta:
        model = ShoppingCart
        fields = "__all__"
        # read_only_fields指明只读字段，即仅用于序列化输出的字段
        read_only_fields = ['cart_item_id']
        # extra_kwargs参数为ModelSerializer添加或修改原有的选项参数
        extra_kwargs = {
            'user_id': {'required': False},
            'goods_id': {'required': False},
            'unit_price': {'required': False}
        }


class MallOrderSerializer(serializers.ModelSerializer):

    class Meta:
        model = MallOrder
        fields = "__all__"
        extra_kwargs = {
            'order_nu': {'required': False},
            'total_price': {'required': False},
            'total_price': {'required': False},
            'pay_status': {'required': False},
            'pay_type': {'required': False},
            'order_status': {'required': False},
            'user_id': {'required': False}
        }


class OrderItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderItem
        fields = "__all__"
        read_only_fields = ['order_item_id']


class OrderAddressSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderAddress
        fields = "__all__"
        read_only_fields = ['order_address_id']


class MallCommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = MallComment
        fields = "__all__"
